﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Web.SystemConfig.Services;
using EPlanet.Web.UI.WebControls;

using System.Data;
using Microsoft.Practices.Unity;
using DreamBuilder.Core.Message;
using CommonModule;
using Web.Views.Public;

namespace Web.Views.SystemConfig
{
    public partial class Auth_Org_User : PageBase
    {
        #region 控件及变量定义
        /// <summary>
        /// Business类
        /// </summary>
        [Dependency]
        public IAuth_Org_User BUSAuth_Org_User
        {
            get;
            set;
        }
        #endregion

        #region PageLoad方法

        private void Page_Load(object sender, System.EventArgs e)
        {
            SetPageProperty();
            InitControlsCultureInfo();
            if (!IsPostBack)
            {
                ShowEdit();
            }
            CreateToolMenu(Toolbar1, ToolMenuType.TYPE_Edit);
            SetToolBarVisible();
            base.CheckEditAuthorities(CurrentPageState);
        }

        /// <summary>
        /// 设置页面的菜单属性
        /// </summary>
        private void SetPageProperty()
        {
            this.DBHelpName = DBHelperString.Dictionary;
            base.MenuID = "ePlanet.Configuration.Auth_Org_User";
            if (!IsPostBack)
            {
                base.CurrentPageState = PageState.State_Edit;
            }
        }

        private void SetToolBarVisible()
        {
            ToolBarHelper.SetToolBarVisible(Toolbar1, ToolBarID.Cancel, false);
        }

        /// <summary>
        /// 显示列表,为列表控件的单据id赋值
        /// </summary>
        private void ShowEdit()
        {
            this.UCChildEdit.ChildEditID = "child_Auth_ORG_User";
            this.UCChildEdit.ParentKey = Request.QueryString.Get("Parent_ID");
        }

        /// <summary>
        /// 初始化控件多语信息
        /// </summary>
        private void InitControlsCultureInfo()
        {
            this.TabControl1.Items[0].Caption = GetString("Auth_Org_User");
            this.litDeptRole.Text = GetString("DeptRole");
        }

        #endregion

        #region OnPreRender方法

        /// <summary>
        /// 重写OnPreRender方法
        /// </summary>
        /// <param name="e"></param>
        protected override void OnPreRender(EventArgs e)
        {
            //根据选择的userid,带过来对应的其他信息
            BUSAuth_Org_User.RefreshChildData(UCChildEdit,UCChildEdit.ViewStateTable);

            string OrgID = Request.QueryString.Get("Parent_ID");
            //初始化角色ListCheckBox
            LoadRoles(OrgID);
            base.OnPreRender(e);
        }

        private void LoadRoles(string OrgID)
        {
            this.chklstRoles.Items.Clear();
            DataTable dt = BUSAuth_Org_User.SelectRolesByOrgID(OrgID);
            ListItem li;

            if (dt != null)
            {
                foreach (DataRow dr in dt.Rows)
                {

                    li = new ListItem((string)dr["Role_Name"]);
                    if (dr["HasRole"].ToString().ToBoolean())
                    {
                        li.Selected = true;
                        //add 1 to the value to save the checked status
                        li.Value = "1_" + dr["Role_ID"].ToString();
                    }
                    else
                    {
                        li.Selected = false;
                        // add 0 to the value to save the checked status
                        li.Value = "0_" + dr["Role_ID"].ToString();
                    }
                    chklstRoles.Items.Add(li);
                }
            }
        }
        #endregion


        #region 事件处理方法
        /// <summary>
        /// 工具条事件处理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Toolbar1_MenuItemClick(object sender, MenuItemClickEventArgs e)
        {
            switch (e.EventItem.ID.ToString().ToLower())
            {
                case "tbsave":
                    this.Validate();
                    if (this.IsValid)
                    {
                        OnSaveData();
                    }
                    break;
                case "tbhelp":
                    break;
            }
        }
        #endregion

        #region 保存
        /// <summary>
        /// 保存数据
        /// </summary>
        private void OnSaveData()
        {
            //构建DataSet
            DataSet dsInfo = new DataSet();
            //取子表，设置外键值，存子表
            DataTable dtChild = this.UCChildEdit.GetChildDataTable();
            this.UCChildEdit.SetFKValue(dtChild, Request.QueryString["Parent_ID"]);
            dsInfo.Tables.Add(dtChild);
            if (dsInfo != null)
            {
                ResultInfo result = BUSAuth_Org_User.SaveData(dsInfo, this.chklstRoles, Request.QueryString["Parent_ID"]);
                if (result.bSuccess == true)
                {
                    base.Alert(GetString("SaveSuccess"));
                }
                else
                {
                    DisplayMessage(result.ErrorDesc);
                }
            }
        }

        #endregion

        #region OnInit
        override protected void OnInit(EventArgs e)
        {
            //
            //CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
            //
            InitializeComponent();
            base.OnInit(e);
        }
        /// <summary>
        /// 添加页面及控件事件
        /// </summary>
        private void InitializeComponent()
        {
            this.Load += new System.EventHandler(this.Page_Load);
            this.Toolbar1.MenuItemClick += new EPlanet.Web.UI.WebControls.MenuItemClickedEventHandler(Toolbar1_MenuItemClick);
        }

        #endregion

    }
}
